#include "activity_view.h"
#include "ui_activity_view.h"

#include "activity_details.h"

#include <QtXml>

ActivityView::ActivityView(QWidget *parent) :
    ViewBase(parent),
    ui(new Ui::ActivityView),
    activityDetails_(0)
{
    ui->setupUi(this);

    QAction *backSoftkeyAction = new QAction(QString("Back"), this);
    backSoftkeyAction->setSoftKeyRole(QAction::NegativeSoftKey);
    QObject::connect(backSoftkeyAction, SIGNAL(triggered()),
                     this, SLOT(close()));
    addAction(backSoftkeyAction);
    connect(ui->cmdDesc, SIGNAL(clicked()),
            this, SLOT(showActivityDetails()));
}

ActivityView::~ActivityView()
{
    delete ui;
    if (activityDetails_) {
      delete activityDetails_;
    }
}

int ActivityView::ProcessRawResult(QByteArray &byte) {
    QImage img = QImage::fromData(byte);
    ui->lblImage->setPixmap(QPixmap::fromImage(img));


  return 0;
}

void ActivityView::IssueRequest() {
  if (handler_) {
    qDebug() << "Requesting Image: " << activity_.show_image_url;
    RequestMethod request(this, activity_.show_image_url);
    handler_->IssueRequest(request);
  }
}

void ActivityView::SetActivity(activity a) {
  activity_ = a;
  ui->lblTitle->setText( a.name );
  ui->lblImage->clear();
  ui->lblStartTime->setText( a.start_time );
  ui->lblEndTime->setText( a.end_time );
  ui->lblAddress->setText( a.address );

  QDomDocument doc;
  doc.setContent(a.description);
  QDomElement e = doc.documentElement();

  ui->cmdDesc->setText( e.text().trimmed() );
}

void ActivityView::showActivityDetails() {
  if (!activityDetails_) {
    activityDetails_ = new ActivityDetails(this);
  }
  activityDetails_->setText(activity_.description);
  activityDetails_->Show();
}
